package thread;

/**
 * @author Jonny Long
 * @date 2021/3/27 17:04
 */
public class Task{
    // 同步方法争夺锁
    private static synchronized void testMethod() {
        try {
            System.out.println(Thread.currentThread().getName()+"\tsleep()开始");
            Thread.sleep(2000L);
            System.out.println(Thread.currentThread().getName()+"\tsleep()结束");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws InterruptedException {
        Thread a = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName()+"\trun()开始");
                testMethod();
                System.out.println(Thread.currentThread().getName()+"\trun()结束");
            }
        }, "a");
        Thread b = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName()+"\trun()开始");
                testMethod();
                System.out.println(Thread.currentThread().getName()+"\trun()结束");
            }
        }, "b");
        System.out.println(Thread.currentThread().getName());
        a.start();
        System.out.println(Thread.currentThread().getName());
        a.join();
        System.out.println(Thread.currentThread().getName());
        b.start();
        System.out.println(Thread.currentThread().getName());
        System.out.println(a.getName() + ":" + a.getState()); // 输出？
        System.out.println(b.getName() + ":" + b.getState()); // 输出？
    }
}
